//
//  FBVideoListDetailMdeol.m
//  MusicForChilden
//
//  Created by qlydfb on 2021/10/29.
//

#import "FBVideoDetailMdeol.h"
#import <AFNetworking.h>
@implementation FBVideoDetailMdeol
+ (void)getVideoDetail:(NSString *)channelId nameId:(NSString *)_id success:(void(^)(FBVideoDetailMdeol *resultDict))success fail:(void(^)(NSError *error))fail{
    NSString *urlStr = [NSString stringWithFormat:@"https://m.qingting.fm/vchannels/%@/programs/%@/",channelId,_id];
//    [QLYDNetWork GET:urlStr Parameters:nil Success:^(id  _Nonnull responseObject) {
//        NSLog(@"respo = %@",responseObject);
//    } Fail:^(NSError * _Nonnull error) {
//        NSLog(@"respo = %@",error);
//;
//    }];
  
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain",nil];
    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
        [manager.requestSerializer setTimeoutInterval:15];
    [manager GET:urlStr parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *repose = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
      //  NSLog(@"respo = %@",repose);
        NSRegularExpression * regular = [[NSRegularExpression alloc]initWithPattern:@"(\"ProgramStore\".*)window.ssr" options: NSRegularExpressionDotMatchesLineSeparators error:nil];

          /*
            设置整体匹配范围 和 匹配属性 包含所写的正则表达式字符串。
            (regular：当做一个被发现正则表达式）
            (htmlStr：当做一个检测文本)
            (NSMakeRange(0,htmlStr.length)：返回检测范围)
            简单的说就是匹配检测文本在指定范围里有多少个 符合正则表达式字符串 属性
          */
        NSArray * htmlArray = [regular matchesInString:repose options:0 range:NSMakeRange(0, repose.length)];
           //NSArray * htmlArray = [regular matchesInString:repose option:0 range:NSMakeRange(0,repose.length)];

            /*
              接下来遍历一下
              NSTextCheckingResult 是一个类用来描述项目位于通过文本检查。每个对象代表了一个请求的文本内容,被发现在分析文本块。
            */
            for (NSTextCheckingResult * match in htmlArray)
            {
              /*
               下面才开始进行匹配
               substringWithRange: 返回一个字符串对象,其中包含的字符的接收器在一个给定的范围内
               range: 返回结果的范围,接收方代表。
              */
                NSString * textStr = [repose substringWithRange: match.range];
                  NSRange range = [textStr rangeOfString:@"window.ssr"];
                  textStr = [NSString stringWithFormat:@"{%@",[textStr substringWithRange:NSMakeRange(0, range.location)]];
                  NSDictionary *info = [NSJSONSerialization JSONObjectWithData:[textStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                  
                  FBVideoDetailMdeol *mModel = [[FBVideoDetailMdeol alloc]initWithDictionary:info[@"ProgramStore"][@"programInfo"] error:nil];
                success(mModel);
            }

;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"respo = %@",error);
 ;
    }];
   
}
@end
